<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 16-12-5
 * Time: 下午9:26
 */
namespace app\wateradmin\controller;

use app\common\controller\BaseAdmin;
use think\console\command\make\Model;

class Adminmember extends BaseAdmin{
    /*
     * 后台首页
     * 赵坤
     * 20161206
     * */
    public function index(){
        $this->assign('title','拼图云会员列表');
        $this->assign('description','拼图云商城');
        if($this->request->param('pagesize')){
            $pagesize = $this->request->param('pagesize');
        }else{
            $pagesize = 15;
        }
        if(isset($_GET['page'])&&!empty($_GET['page'])){
            $page = intval($_GET['page']);
        }else{
            $page = 1;
        }
        $condition = '';
        if(isset($_GET['start_time'])&&!empty($_GET['start_time'])&&isset($_GET['end_time'])&&!empty($_GET['end_time'])){
            $start_time=strtotime($_GET['start_time']);
            $end_time=strtotime($_GET['end_time']);
            $condition = ' (m1.member_time >='.$start_time.' AND m1.member_time <='.$end_time.')';
        }
        if(isset($_GET['key'])){
            $key = trim($_GET['key']);
            $condition = " (m1.member_name  like '%".$key."%' OR m1.member_id=".intval($key).")";
        }
        //商城统计信息
        $res = model('ShopMember')->getPageMember($condition , '',$page, $pagesize);;//用户总数
        $this->assign('pagesion',$res['pagesion']);
        $this->assign('memberlist',$res['list']);
        $this->assign('member_count',$res['count']);
        return $this->fetch();
    }
    /*
     * 搜索会员
     * 赵坤
     * 20161213
     * */
    public function selectMember(){

    }
    /*
    * 编辑会员
    * 赵坤
    * 20161210
    * */
    public function edit(){
        $this->assign('title','拼图云会员编辑');
        $this->assign('description','拼图云商城');
        $id = intval($this->request->param('member_id'));
        if($id){
            $memberInfo = model('ShopMember')->getMemberInfo(array('member_id'=>$id), '*' );
            if(!empty($memberInfo)){
                $this->assign('memberInfo',$memberInfo);
            }
        }
        if($this->request->isPost()){
            $memberdata = array(

            );
        }
        return $this->fetch();
    }
    /*
     * 添加会员
     * 赵坤
     * 20161210
     * */
    public function add(){
        $this->assign('title','拼图云会员添加');
        $this->assign('description','拼图云商城');
        if($this->request->isPost()){
            $member_info = array(
                'member_name'=>$this->request->param('member_name'),
                'member_truename'=>$this->request->param('member_truename'),
                'member_avatar'=>$this->request->param('headimg'),
                'member_mobile'=>$this->request->param('member_mobile'),
                'member_store'=>session('shop')
            );
            $member_id = model('ShopMember')->addMember($member_info);
            if($member_id){
                $this->addlog(getLanguage('addmember').'[会员名:'.$member_info['member_name'].'][操作员:'.session('seller_name').']');
                return json(array('status'=>1));
            }else{
                return json(array('status'=>0));
            }
        }
        return $this->fetch();
    }
    /*
     * 会员充值
     * 赵坤
     * 20161210
     * */
    public function recharge(){
        if($this->request->isPost()){
            if($this->request->param('type')==1){
                //积分
                $member_id = $this->request->param('credit_id');
                $member = model('ShopMember')->getMemberInfo(array('member_id'=>intval($member_id)));
                if($member){
                    $member['amount'] = intval($this->request->param('amount'));
                    $type = intval($this->request->param('credittype'));
                    $creditmessage  =$this->request->param('creditmessage');
                    if($type==1){
                        $param = array('member_points'=>array('exp','member_points+'.$member['amount']));
                        $res = model('ShopMember')->updateMember($param,$member_id);
                    }elseif ($type==2){
                        $param = array('member_points'=>array('exp','member_points-'.$member['amount']));
                        $res = model('ShopMember')->updateMember($param,$member_id);
                        $member['amount'] = -$member['amount'];
                    }
                    if($res===false){
                        return json(array('status'=>0,'info'=>'充值失败'));
                    }else{
                        //记录日志
                        $log_info = getLanguage('rechargemember').'[会员名:'.$member['member_name'].'][积分:'.$member['amount'].'][操作员:'.session('seller_name').'][备注:'.$creditmessage.']';

                        $this->addlog($log_info);

                        return json(array('status'=>1,'info'=>'充值成功'));
                    }
                }else{
                    return json(array('status'=>0,'info'=>'用户信息未取到'));
                }


            }elseif ($this->request->param('type')==2){
                $member_id = $this->request->param('money_id');
                $member = model('ShopMember')->getMemberInfo(array('member_id'=>intval($member_id)));
                if($member){
                    $member['amount'] = floatval($this->request->param('amount'));
                    $member['pdr_sn'] = $this->request->param('pdr_sn');
                    $moneytype = intval($this->request->param('moneytype'));
                    $moneymessage = $this->request->param('moneymessage');
                    if($moneytype==1){
                        $res = model('ShopMember')->recharge($member);
                    }elseif($moneytype==2){
                        $member['amount'] = -$member['amount'];
                        $res = model('ShopMember')->recharge($member);
                    }

                    if($res===false){
                        return json(array('status'=>0,'info'=>'充值失败'));
                    }else{
                        //记录日志
                        $log_info = getLanguage('rechargemember').'[会员名:'.$member['member_name'].'][余额:'.$member['amount'].'][操作员:'.session('seller_name').'][备注:'.$moneymessage.']';
                        $this->addlog($log_info);

                        return json(array('status'=>1,'info'=>'充值成功'));
                    }
                }else{
                    return json(array('status'=>0,'info'=>'用户信息未取到'));
                }
            }

        }else{
            $this->error('错误页面访问');
        }

    }
    /*
     * 会员删除
     * 赵坤
     * 20161221
     * */
    public function del(){
        $ids = $_POST['member_id'];
        if(is_array($ids)&&!empty($ids)){
            $res = model('ShopMember')->del($ids);
            if($res!==false){
                $ids = implode(',',$ids);
                $log_info = getLanguage('addmember').'[ID:'.$ids.'][操作员:'.session('seller_name').']';
                $this->addlog($log_info);
                return json(array('status'=>1,'info'=>'删除成功'));
            }else{
                return json(array('status'=>1,'info'=>'删除失败'));
            }
        }else{
            $id = intval($ids);
            $res = model('ShopMember')->del($id);
            if($res!==false){
                $log_info = getLanguage('addmember').'[ID:'.$id.'][操作员:'.session('seller_name').']';
                $this->addlog($log_info);
                return json(array('status'=>1,'info'=>'删除成功'));
            }else{
                return json(array('status'=>1,'info'=>'删除失败'));
            }
        }
    }
    /*
     * 会员操作
     * 赵坤
     * 20161221
     * */
    public function operatemember(){


        $ids = $_POST['member_id'];
        if(is_array($ids)&&!empty($ids)){
            $is_black = intval($this->request->param('is_black'));
            $res = model('ShopMember')->updateMember(array('member_state'=>$is_black),$ids);
            if($res!==false){
                $ids = implode(',',$ids);
                $log_info = getLanguage('blackmember').'[ID:'.$ids.']';
                $this->addlog($log_info);
                return json(array('status'=>1,'info'=>'执行成功'));
            }else{
                return json(array('status'=>0,'info'=>'操作失败'));
            }
        }
        return json(array('status'=>1,'info'=>'执行成功'));
    }


}







